﻿using Cordyceps.Micro.AuthServer.Dimain.Models;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace Cordyceps.Micro.AuthServer.EntityFrameworkCore;

public class AuthDbContext : IdentityDbContext<
    CordycepsIdentityUser,
    CordycepsIdentityRole,
    Guid,
    CordycepsIdentityUserClaim,
    CordycepsIdentityUserRole,
    CordycepsIdentityUserLogin,
    CordycepsIdentityRoleClaim,
    CordycepsIdentityUserToken>
// CordycepsDbContextBase<AuthDbContext>
{
    /// <summary>
    ///     关键：这个构造函数允许从外部传入配置（如连接字符串）
    /// </summary>
    /// <param name="options"></param>
    public AuthDbContext(DbContextOptions<AuthDbContext> options) : base(options)
    {
    }

    //public virtual DbSet<CordycepsOpenIddictApplications> CordycepsApplications { get; set; }
    //public virtual DbSet<CordycepsOpenIddictAuthorizations> CordycepsAuthorizations { get; set; }
    //public virtual DbSet<CordycepsOpenIddictScopes> CordycepsScopes { get; set; }
    //public virtual DbSet<CordycepsOpenIddictTokens> CordycepsTokens { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        // ⚠️ 关键：显式注册 OpenIddict 实体
        // 即使你没自定义实体，也建议调用 UseOpenIddict()
        // modelBuilder.UseOpenIddict();

        modelBuilder.ConfigureAuthServer();
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
#if DEBUG
        optionsBuilder.EnableSensitiveDataLogging();
#endif
        base.OnConfiguring(optionsBuilder);
    }
}